package base;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 连接数据库
 * @author scott_wrl
 */
public class DBUtil {
	/**
	 * 2.获取Oracle数据库连接
	 * @return	数据库的连接对象 Connection
	 * @throws Exception
	 */
	public static Connection getConnectionForOracle(){
		Properties properties = new Properties();
		try {
			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String driver = properties.getProperty("oracle.jdbc.driver");
		String url = properties.getProperty("oracle.jdbc.url");
		String user = properties.getProperty("oracle.jdbc.username");
		String password = properties.getProperty("oracle.jdbc.password");
		
		Connection conn = null;
		try {
			//1.加载驱动：连接mysql和Oracle的驱动不一样
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			System.err.println("驱动加载失败...");
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println("数据库连接失败...");
			e.printStackTrace();
		}
		System.out.println("连接成功..");
		return conn;
	}
	/**
	 * 1.获取mysql数据库连接
	 * @return	数据库的连接对象 Connection
	 * @throws Exception
	 */
	public static Connection getConnectionForMySQL(){
		Properties properties = new Properties();
		try {
			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String driver = properties.getProperty("mysql.jdbc.driver");
		String url = properties.getProperty("mysql.jdbc.url");
		String user = properties.getProperty("mysql.jdbc.username");
		String password = properties.getProperty("mysql.jdbc.password");
		
		Connection connection = null;
		try {
			//1.加载驱动：连接mysql和Oracle的驱动不一样
			Class.forName(driver);
			connection = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			System.err.println("驱动加载失败...");
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println("数据库连接失败...");
			e.printStackTrace();
			
		}
		return connection;
	}
	
	/**
	 * 2.关闭资源
	 * @param rs	关闭结果集  ResultSet
	 * @param ps	关闭预编译  PreparedStatement
	 * @param conn	关闭数据库连接
	 */
	public static void closeResource(ResultSet rs,PreparedStatement ps,Connection conn){
		if(null != rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		closeResource(ps, conn);
	}
	
	/**
	 * 1.关闭资源
	 * @param ps	关闭预编译  PreparedStatement
	 * @param conn	关闭数据库连接
	 */
	public static void closeResource(PreparedStatement ps,Connection conn){
		if(null != ps){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != conn){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
